/*
    针对 object 对象定制化的js
    基于customize.js
 */
var tmsObjectId = 1;
function getTmsObjectIdNext(){
    return ++tmsObjectId;
}
var tmsObjectValue = {};
tmsObject = {

    setCombo:function(jq,objectId,objectCode,objectName){
        jq.combo('setValue',objectId);
        jq.combo('setText',objectName+"("+objectCode+")");
    }
    ,treeObject:function(jq,param1,param2){
        var treeJq = jq.data('treeJq');
        return treeJq.tree(param1,param2);
    }
    ,treeCombo:function (jq,objectCodes,required){
        if(isBlank(required)){required = false;}
        if(isBlank(objectCodes)){
            console.error("Invalid param");
            return;
        }
        if(jq.data('init')== 'complete'){return;};
        jq.attr('tmsObjectId',getTmsObjectIdNext());
        jq.combo({
            width:180
            ,required:required
            ,editable:false
        });
        var panel = jq.combo('panel');
        var clearButton = $('<a ></a>');
        panel.append(clearButton);
        clearButton.linkbutton({
            width:25
            ,height:25
            ,iconCls:'icon-clear'
            ,onClick:function(){
                jq.combo('clear');
            }
        });
        var treeJq = $('<ul></ul>');
        treeJq.data('valueTarget',jq);

        var callFn = function (node) {
            if(node.info.type == 1 || node.info.type == 3 ){
                easyuiAlert("不能选择文件夹!",'error');
                return;
            }
            tmsObjectValue.tmsObjectid = node;
            jq.combo('setValue',node.id);
            jq.combo('setText',node.text);
            jq.combo('hidePanel');
        };
        tmsObject._getTreeRootObjects(panel,treeJq,objectCodes,callFn);

        jq.data('init','complete');
    },
    tree:function(jq,objectCodes,clickFunction){
        if(jq.data('initObjectTree') == 'complete'){
            var treeJq = jq.data('treeJq');
            tmsObject._getTreeRootObjects(jq,treeJq,objectCodes,clickFunction,true);
            return ;
        }
        var treeJq = $('<ul></ul>');
        jq.data('treeJq',treeJq);
        tmsObject._getTreeRootObjects(jq,treeJq,objectCodes,clickFunction);
        jq.data('initObjectTree','complete');
    }
    ,_createTree:function(treeJq,queryParam,callFn){
        if(isBlank(queryParam)){
            console.error("Invalid param");
            return;
        }
        var url = basePath+'objectAction/searchObjectWithTree.json';
        treeJq.tree({
            url:url
            ,checkbox:false
            ,lines:true
            ,queryParams:queryParam
            ,method:"post"
            ,onClick:function(node){
                if(typeof callFn == 'function'){
                    callFn(node);
                }
            }
            ,formatter:function(node){
                if(node.info.companyId == 0){
                    return node.text;
                }
                var tooltip = null;
                if(node.info.company.status == 0){
                    tooltip ='<spa class="color:red">(未补全所属公司信息)</spa>';
                }else{
                    tooltip = node.info.company.companyName;
                }
                return node.text + ' --- '+tooltip;
            }
        });

    }
    ,_getTreeRootObjects:function (panel,treeJq,objectCodes,callFn,isReload) {
        var param = objectCodes;
        tmsAjax({
            url:basePath+'objectAction/findObjectByCodes.json'
            ,data:JSON.stringify(param)
            ,async:false
            ,type:'post'
            ,success:function (data) {
                var totalCodes = getTotalCodes(data);
                if(!isReload){
                   var searchBox = tmsObject._createSeachBox(panel,treeJq,{"totalCodes":totalCodes});
                    panel.append(treeJq);
                    panel.data('searchBox',searchBox);
                }else{
                    var searchBox = panel.data('searchBox');
                    searchBox.data('treeParam',{"totalCodes":totalCodes});
                }
                tmsObject._createTree(treeJq,{"totalCodes":totalCodes},callFn);
            }
        });
    }
    ,_createSeachBox:function(panel,treeJq,treeParam){
        var searchBox = $('<input />');
        panel.append(searchBox);
        searchBox.data('treeParam',treeParam);
        searchBox.textbox({
            buttonIcon:'icon-search'
            ,prompt:'关键字'
            ,onClickButton:function(){
                var search=searchBox.textbox('getValue');
                var param = searchBox.data('treeParam');
                param.search = search;
                tmsObject._createTree(treeJq,param);
            }
        });
        return searchBox;
    }
};

function getTotalCodes(objectList){
    var result = [];
    $.each(objectList,function (index,object) {
        result.push(object.totalCode);
    });
    return result;
}
